﻿using ICSharpCode.SharpZipLib.GZip;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.Networking;
using UnityEngine.UI;

public class ReadyRoomScene : NetworkBehaviour
{
    [CompilerGenerated]
    private static Action <>f__am$cache10;
    [CompilerGenerated]
    private static Action <>f__am$cache11;
    [SerializeField]
    private GameObject chat;
    [SerializeField]
    private InputField chatInputField;
    [SerializeField]
    private Text chatLogText;
    [SerializeField]
    private GameObject hint;
    [SerializeField]
    private Text hintText;
    private static int kRpcRpcFadeoutLaunchSound;
    private static int kRpcRpcStartGame;
    [SerializeField]
    private AudioSource launchSound;
    [SerializeField]
    private Toggle levelElement;
    [SerializeField]
    private RectTransform levelGroup;
    private TextMesh nameText;
    public const string SceneName = "ReadyRoom";
    [SerializeField]
    private Button startButton;
    private bool started;
    private NetworkTransmitter transmitter;
    [SerializeField]
    private GameObject uiRoot;
    [SerializeField]
    private GameObject window;

    public static  event Action onLoaded;

    public static  event Action onStartGame;

    static ReadyRoomScene()
    {
        if (<>f__am$cache10 == null)
        {
            <>f__am$cache10 = new Action(ReadyRoomScene.<onLoaded>m__D1);
        }
        onLoaded = <>f__am$cache10;
        if (<>f__am$cache11 == null)
        {
            <>f__am$cache11 = new Action(ReadyRoomScene.<onStartGame>m__D2);
        }
        onStartGame = <>f__am$cache11;
        kRpcRpcStartGame = -1662219632;
        NetworkBehaviour.RegisterRpcDelegate(typeof(ReadyRoomScene), kRpcRpcStartGame, new NetworkBehaviour.CmdDelegate(ReadyRoomScene.InvokeRpcRpcStartGame));
        kRpcRpcFadeoutLaunchSound = -595652914;
        NetworkBehaviour.RegisterRpcDelegate(typeof(ReadyRoomScene), kRpcRpcFadeoutLaunchSound, new NetworkBehaviour.CmdDelegate(ReadyRoomScene.InvokeRpcRpcFadeoutLaunchSound));
        NetworkCRC.RegisterBehaviour("ReadyRoomScene", 0);
    }

    [CompilerGenerated]
    private static void <onLoaded>m__D1()
    {
    }

    [CompilerGenerated]
    private static void <onStartGame>m__D2()
    {
    }

    public void CallRpcFadeoutLaunchSound()
    {
        if (!NetworkServer.active)
        {
            UnityEngine.Debug.LogError("RPC Function RpcFadeoutLaunchSound called on client.");
        }
        else
        {
            NetworkWriter writer = new NetworkWriter();
            writer.Write((short) 0);
            writer.Write((short) ((ushort) 2));
            writer.WritePackedUInt32((uint) kRpcRpcFadeoutLaunchSound);
            writer.Write(base.GetComponent<NetworkIdentity>().netId);
            this.SendRPCInternal(writer, 0, "RpcFadeoutLaunchSound");
        }
    }

    public void CallRpcStartGame()
    {
        if (!NetworkServer.active)
        {
            UnityEngine.Debug.LogError("RPC Function RpcStartGame called on client.");
        }
        else
        {
            NetworkWriter writer = new NetworkWriter();
            writer.Write((short) 0);
            writer.Write((short) ((ushort) 2));
            writer.WritePackedUInt32((uint) kRpcRpcStartGame);
            writer.Write(base.GetComponent<NetworkIdentity>().netId);
            this.SendRPCInternal(writer, 0, "RpcStartGame");
        }
    }

    private byte[] Compress(byte[] bytes)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            GZipOutputStream stream2 = new GZipOutputStream(stream);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.Close();
            return stream.ToArray();
        }
    }

    private string GetLevelDisplayName(Level level)
    {
        string shortName = level.asset.shortName;
        if (!level.asset.isBuiltinAsset)
        {
            shortName = shortName + string.Format(" <color=#AAAAAA>({0})</color>", level.asset.author);
        }
        return shortName;
    }

    protected static void InvokeRpcRpcFadeoutLaunchSound(NetworkBehaviour obj, NetworkReader reader)
    {
        if (!NetworkClient.active)
        {
            UnityEngine.Debug.LogError("RPC RpcFadeoutLaunchSound called on server.");
        }
        else
        {
            ((ReadyRoomScene) obj).RpcFadeoutLaunchSound();
        }
    }

    protected static void InvokeRpcRpcStartGame(NetworkBehaviour obj, NetworkReader reader)
    {
        if (!NetworkClient.active)
        {
            UnityEngine.Debug.LogError("RPC RpcStartGame called on server.");
        }
        else
        {
            ((ReadyRoomScene) obj).RpcStartGame();
        }
    }

    public static YieldInstruction Load()
    {
        return NetworkUtility.LoadScene("ReadyRoom");
    }

    private void OnChatLogsChanged()
    {
        this.chatLogText.text = string.Join("\n", SteamNetworkManager.chatLogs.ToArray());
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
    }

    private void OnDisable()
    {
        SteamNetworkManager.onChatLogsChanged -= new Action(this.OnChatLogsChanged);
    }

    private void OnEnable()
    {
        SteamNetworkManager.onChatLogsChanged += new Action(this.OnChatLogsChanged);
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        bool flag;
        return flag;
    }

    [ClientRpc]
    private void RpcFadeoutLaunchSound()
    {
        this.StartAnimateFloat(1f, 0f, 2f, (Action<float>) (v => (this.launchSound.volume = v)), null);
    }

    [ClientRpc]
    private void RpcStartGame()
    {
        this.hint.SetActive(true);
        this.hintText.text = "Waiting...";
        this.started = true;
        this.launchSound.gameObject.SetActive(true);
    }

    private void SendFiles()
    {
    }

    [DebuggerHidden]
    private IEnumerator Start()
    {
        return new <Start>c__IteratorD { <>f__this = this };
    }

    private void UNetVersion()
    {
    }

    private void Update()
    {
        if (Camera.main != null)
        {
            RaycastHit hit;
            if (this.nameText == null)
            {
                this.nameText = GameResources.NameText.CloneGameObject<TextMesh>();
            }
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, float.PositiveInfinity, (int) GameLayers.Character))
            {
                this.nameText.gameObject.SetActive(true);
                this.nameText.transform.position = hit.collider.bounds.GetBottom();
                this.nameText.text = hit.collider.name;
            }
            else
            {
                this.nameText.gameObject.SetActive(false);
            }
        }
    }

    public static string selectedLevelName
    {
        get
        {
            return PlayerPrefs.GetString(typeof(ReadyRoomScene).Name + ".selectedLevelName");
        }
        private set
        {
            PlayerPrefs.SetString(typeof(ReadyRoomScene).Name + ".selectedLevelName", value);
        }
    }

    [CompilerGenerated]
    private sealed class <Start>c__IteratorD : IDisposable, IEnumerator, IEnumerator<object>
    {
        internal object $current;
        internal int $PC;
        private static Func<Respawn, Vector3> <>f__am$cache6;
        private static Action<ReadyRoomReadyTrigger> <>f__am$cache7;
        private static TriggerEventHandler <>f__am$cache8;
        private static TriggerEventHandler <>f__am$cache9;
        private static Func<bool> <>f__am$cacheA;
        private static Func<Player, bool> <>f__am$cacheB;
        private static Action <>f__am$cacheC;
        internal ReadyRoomScene <>f__this;
        internal List<Level> <levels>__2;
        internal Vector3 <respawn>__0;
        internal TriggerEventListener <uiTrigger>__1;

        private static Vector3 <>m__D4(Respawn x)
        {
            return (x.transform.position + UnityEngine.Random.insideUnitSphere.SetY(0f));
        }

        private static void <>m__D5(ReadyRoomReadyTrigger x)
        {
            TriggerEventListener listener = x.AddComponent<TriggerEventListener>();
            if (<>f__am$cache8 == null)
            {
                <>f__am$cache8 = delegate (Collider co) {
                    if (co.gameObject == PlayerCharacter.local.gameObject)
                    {
                        Player.local.SetIsReady(true);
                    }
                };
            }
            listener.onTriggerEnter = <>f__am$cache8;
            if (<>f__am$cache9 == null)
            {
                <>f__am$cache9 = delegate (Collider co) {
                    if (co.gameObject == PlayerCharacter.local.gameObject)
                    {
                        Player.local.SetIsReady(false);
                    }
                };
            }
            listener.onTriggerExit = <>f__am$cache9;
        }

        internal void <>m__D6(Collider co)
        {
            if ((co.gameObject == PlayerCharacter.local.gameObject) && !this.<>f__this.started)
            {
                this.<>f__this.window.SetActive(true);
            }
        }

        internal void <>m__D7(Collider co)
        {
            if (co.gameObject == PlayerCharacter.local.gameObject)
            {
                this.<>f__this.window.SetActive(false);
            }
        }

        internal void <>m__D8(Toggle e, int i)
        {
            <Start>c__AnonStorey64 storey = new <Start>c__AnonStorey64 {
                <>f__ref$13 = this,
                i = i
            };
            Level level = this.<levels>__2[storey.i];
            e.isOn = ReadyRoomScene.selectedLevelName == level.name;
            e.SetTextAndListener(this.<>f__this.GetLevelDisplayName(level), new UnityAction<bool>(storey.<>m__DF));
        }

        internal void <>m__D9()
        {
            ReadyRoomScene.onStartGame();
            this.<>f__this.CallRpcStartGame();
            this.<>f__this.window.SetActive(false);
            this.<>f__this.SendFiles();
            if (<>f__am$cacheA == null)
            {
                <>f__am$cacheA = delegate {
                    if (<>f__am$cacheB == null)
                    {
                        <>f__am$cacheB = x => !x.isReady;
                    }
                    return Player.all.Count<Player>(<>f__am$cacheB) == 0;
                };
            }
            this.<>f__this.StartWaitUntil(<>f__am$cacheA, delegate {
                this.<>f__this.CallRpcFadeoutLaunchSound();
                if (<>f__am$cacheC == null)
                {
                    <>f__am$cacheC = (Action) (() => IngameScene.Load(ReadyRoomScene.selectedLevelName));
                }
                this.<>f__this.StartWaitForSeconds(2f, <>f__am$cacheC);
            });
        }

        internal void <>m__DA(int id, byte[] bytes)
        {
            NetworkTransmitter.TransmissionData data = this.<>f__this.transmitter.clientTransmissionData[id];
            this.<>f__this.hint.SetActive(true);
            this.<>f__this.hintText.text = string.Format("Receiving level data: {0}KB/{1}KB", data.curDataIndex / 0x400, data.data.Length / 0x400);
        }

        internal void <>m__DB(int id, byte[] bytes)
        {
            this.<>f__this.hint.SetActive(false);
        }

        internal void <>m__DC()
        {
            if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
            {
                if (EventSystem.current.currentSelectedGameObject == this.<>f__this.chatInputField.gameObject)
                {
                    if (!string.IsNullOrEmpty(this.<>f__this.chatInputField.text))
                    {
                        SteamNetworkManager.SendChatMsg(PlayerCharacter.local.name + ": " + this.<>f__this.chatInputField.text);
                    }
                    this.<>f__this.chatInputField.text = string.Empty;
                    EventSystem.current.SetSelectedGameObject(null);
                }
                else
                {
                    EventSystem.current.SetSelectedGameObject(this.<>f__this.chatInputField.gameObject);
                    this.<>f__this.chatInputField.ActivateInputField();
                }
            }
            if (PlayerCharacter.local != null)
            {
                PlayerCharacter.local.enabled = !this.<>f__this.chatInputField.isFocused;
            }
        }

        private static void <>m__DD(Collider co)
        {
            if (co.gameObject == PlayerCharacter.local.gameObject)
            {
                Player.local.SetIsReady(true);
            }
        }

        private static void <>m__DE(Collider co)
        {
            if (co.gameObject == PlayerCharacter.local.gameObject)
            {
                Player.local.SetIsReady(false);
            }
        }

        private static bool <>m__E0()
        {
            if (<>f__am$cacheB == null)
            {
                <>f__am$cacheB = x => !x.isReady;
            }
            return (Player.all.Count<Player>(<>f__am$cacheB) == 0);
        }

        internal void <>m__E1()
        {
            this.<>f__this.CallRpcFadeoutLaunchSound();
            if (<>f__am$cacheC == null)
            {
                <>f__am$cacheC = (Action) (() => IngameScene.Load(ReadyRoomScene.selectedLevelName));
            }
            this.<>f__this.StartWaitForSeconds(2f, <>f__am$cacheC);
        }

        private static bool <>m__E2(Player x)
        {
            return !x.isReady;
        }

        private static void <>m__E3()
        {
            IngameScene.Load(ReadyRoomScene.selectedLevelName);
        }

        [DebuggerHidden]
        public void Dispose()
        {
            this.$PC = -1;
        }

        public bool MoveNext()
        {
            uint num = (uint) this.$PC;
            this.$PC = -1;
            switch (num)
            {
                case 0:
                    ScreenFader.CoverUp();
                    if (Player.local != null)
                    {
                        break;
                    }
                    this.$current = Player.Create();
                    this.$PC = 1;
                    goto Label_0279;

                case 1:
                    break;

                case 2:
                    goto Label_00C6;

                default:
                    goto Label_0277;
            }
            Player.local.SetIsReady(false);
            if (Player.local.character == null)
            {
                if (<>f__am$cache6 == null)
                {
                    <>f__am$cache6 = new Func<Respawn, Vector3>(ReadyRoomScene.<Start>c__IteratorD.<>m__D4);
                }
                this.<respawn>__0 = Respawn.all.Select<Respawn, Vector3>(<>f__am$cache6).GetRandomElement<Vector3>();
                this.$current = Player.local.CreateCharacter(this.<respawn>__0);
                this.$PC = 2;
                goto Label_0279;
            }
        Label_00C6:
            ReadyRoomScene.onLoaded();
            if (<>f__am$cache7 == null)
            {
                <>f__am$cache7 = new Action<ReadyRoomReadyTrigger>(ReadyRoomScene.<Start>c__IteratorD.<>m__D5);
            }
            UnityEngine.Object.FindObjectsOfType<ReadyRoomReadyTrigger>().Foreach<ReadyRoomReadyTrigger>(<>f__am$cache7);
            this.<>f__this.window.SetActive(false);
            if (NetworkUtility.isServer)
            {
                this.<uiTrigger>__1 = UnityEngine.Object.FindObjectOfType<ReadyRoomUITrigger>().AddComponent<TriggerEventListener>();
                this.<uiTrigger>__1.onTriggerEnter = new TriggerEventHandler(this.<>m__D6);
                this.<uiTrigger>__1.onTriggerExit = new TriggerEventHandler(this.<>m__D7);
            }
            this.<>f__this.hint.SetActive(false);
            if (NetworkUtility.isServer)
            {
                this.<levels>__2 = Level.GetAllLevels().ToList<Level>();
                UIUtility.RefreshElements<Toggle>(this.<>f__this.levelElement, this.<levels>__2.Count, this.<>f__this.levelGroup, new Action<Toggle, int>(this.<>m__D8));
            }
            if (NetworkUtility.isServer)
            {
                this.<>f__this.startButton.SetListener(new UnityAction(this.<>m__D9));
            }
            this.<>f__this.transmitter = this.<>f__this.GetComponent<NetworkTransmitter>();
            this.<>f__this.transmitter.OnDataFragmentReceived += new Action<int, byte[]>(this.<>m__DA);
            this.<>f__this.transmitter.OnDataCompletelyReceived += new Action<int, byte[]>(this.<>m__DB);
            if (SteamNetworkManager.active)
            {
                this.<>f__this.chat.SetActive(true);
                this.<>f__this.StartUpdate(new Action(this.<>m__DC));
            }
            else
            {
                this.<>f__this.chat.SetActive(false);
            }
            ScreenFader.Fadeout();
            this.$PC = -1;
        Label_0277:
            return false;
        Label_0279:
            return true;
        }

        [DebuggerHidden]
        public void Reset()
        {
            throw new NotSupportedException();
        }

        object IEnumerator<object>.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }

        object IEnumerator.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }

        private sealed class <Start>c__AnonStorey64
        {
            internal ReadyRoomScene.<Start>c__IteratorD <>f__ref$13;
            internal int i;

            internal void <>m__DF(bool v)
            {
                if (v)
                {
                    ReadyRoomScene.selectedLevelName = this.<>f__ref$13.<levels>__2.ElementAt<Level>(this.i).name;
                }
            }
        }
    }
}

